﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Server
{
    public partial class Danhsachsanpham : Form
    {
        public Danhsachsanpham()
        {
            InitializeComponent();
        }

        #region "Biến"
        public delegate void CapNhatSanPhamHandler(CProduct sp, EventArgs e);
        public event CapNhatSanPhamHandler CapNhatSanPhamEvent;
        List<CProduct> list;
        #endregion

        #region "Event"
        /// <summary>
        /// Load danh sách sản phẩm 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Danhsachsanpham_Load(object sender, EventArgs e)
        {
            CXuLyXML xulyxml = new CXuLyXML();
            list = xulyxml.LayDanhSachSanPham();
            listSanPham.DataSource = list;
            listSanPham.ValueMember = "Ma";
            listSanPham.DisplayMember = "Ten";
            lblTongSanPham.Text = "Count: " + list.Count + " sản phẩm";
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Refresh_Click(object sender, EventArgs e)
        {
            Danhsachsanpham_Load(sender, e);
        }
        /// <summary>
        /// Thoát
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// Chọn sản phẩm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            CProduct product = listSanPham.SelectedItem as CProduct;
            txtDateTime.Value = product.Ngaydang;
            txtGiaTien.Value = decimal.Parse(product.Giatien.ToString());
            txtMa.Text = product.Ma.ToString();
            txtMoTa.Text = product.Mota;
            txtTen.Text = product.Ten;
        }
        /// <summary>
        /// Sửa sản phẩm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSua_Click(object sender, EventArgs e)
        {
            if(txtTen.Enabled == false)
            {
                An_Hien_Control(true);
            }
            else
            {
                CProduct producupdate = new CProduct();
                producupdate.Ma =int.Parse(txtMa.Text);
                producupdate.Mota = txtMoTa.Text;
                producupdate.Giatien = double.Parse(txtGiaTien.Value.ToString());
                producupdate.Ten = txtTen.Text;
                producupdate.Ngaydang = txtDateTime.Value;

                CXuLyXML xulyxml = new CXuLyXML();
                xulyxml.CapNhatSanPham(producupdate);

                Danhsachsanpham_Load(sender, e);

                //Cập nhật sản phẩm
                if(CapNhatSanPhamEvent != null)
                {
                    CapNhatSanPhamEvent(producupdate, new EventArgs());
                }
                An_Hien_Control(false);
            }
        }
        /// <summary>
        /// Hủy sửa
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnHuy_Click(object sender, EventArgs e)
        {
            An_Hien_Control(false);
            listSanPham_SelectedIndexChanged(sender, e);
        }

        #endregion
         
        #region "Hàm"
        public void An_Hien_Control (bool flag)
        {
            txtDateTime.Enabled = flag;
            txtGiaTien.Enabled = flag;
            txtMoTa.Enabled = flag;
            txtTen.Enabled = flag;
        }

        #endregion
   
    }
}
